package com.yxq.servlet;

import com.yxq.dao.ShopDAO;
import com.yxq.dao.UserDAO;
import com.yxq.entity.FruitBean;
import com.yxq.entity.GoodsSingle;
import com.yxq.entity.UserBean;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 *
 */
public class ShoppingServlet extends HttpServlet
{
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();

        //获得请求资源路径
        String uri = request.getRequestURI();
        String action = uri.substring(uri.lastIndexOf("/"), uri.lastIndexOf("."));

        try
        {
            if ("/login".equals(action))
            {
                String username = request.getParameter("username");
                String password = request.getParameter("password");
                UserDAO userDao = new UserDAO();
                UserBean user = userDao.findByName(username);
                String status;

                if (user == null)
                {
                    status = "1";
                } else if (!user.getPassword().equals(password))
                {
                    status = "2";
                } else
                {
                    status = "0";
                }
                out.print(status);
            }
            if ("/list".equals(action))
            {
                ShopDAO shopDAO = new ShopDAO();
                List<FruitBean> list = shopDAO.findAllFruits();
                request.setAttribute("list", list);
                request.getRequestDispatcher("admin/index.jsp").forward(request, response);
            }
            if ("/shopcar".equals(action))
            {
                String name = request.getParameter("name");
                Integer num = Integer.parseInt(request.getParameter("num"));
                ShopDAO shopDAO = new ShopDAO();

                shopDAO.addFruit(name, num);
                System.out.println(name);

                List<GoodsSingle> list = shopDAO.findAllGoodsSingles();

                double total = shopDAO.totalMoney();

                request.setAttribute("total", total);
                request.setAttribute("list", list);
                request.getRequestDispatcher("admin/shopcar.jsp").forward(request, response);
            }
            if ("/shopcarDel".equals(action))
            {
                String name = request.getParameter("name");
                Integer num = Integer.parseInt(request.getParameter("num"));
                ShopDAO shopDAO = new ShopDAO();

                if (name != null)
                {
                    shopDAO.addFruit(name, num);

                    List<GoodsSingle> list = shopDAO.findAllGoodsSingles();

                    double total = shopDAO.totalMoney();

                    request.setAttribute("total", total);
                    request.setAttribute("list", list);
                    request.getRequestDispatcher("admin/shopcar.jsp").forward(request, response);
                } else
                {
                    request.getRequestDispatcher("admin/shopcar.jsp").forward(request, response);
                }
            }
            if ("/shopcarClear".equals(action))
            {
                ShopDAO shopDAO = new ShopDAO();

                List<GoodsSingle> list = shopDAO.findAllGoodsSingles();

                request.setAttribute("list", list);
                request.getRequestDispatcher("admin/shopcar.jsp").forward(request, response);
            }
            if ("/delete".equals(action))
            {
                String name = request.getParameter("name");
                ShopDAO shopDAO = new ShopDAO();

                shopDAO.delete(name);

                List<GoodsSingle> list = shopDAO.findAllGoodsSingles();
                double total = shopDAO.totalMoney();

                request.setAttribute("total", total);
                request.setAttribute("list", list);
                request.getRequestDispatcher("admin/shopcar.jsp").forward(request, response);
            }
            if ("/deleteAll".equals(action))
            {
                ShopDAO shopDAO = new ShopDAO();
                shopDAO.deleteAll();
                response.sendRedirect("shopcarClear.do");
            }
            if ("/deleteOneLine".equals(action))
            {
                String name = request.getParameter("name");
                ShopDAO shopDAO = new ShopDAO();

                shopDAO.deleteOne(name);

                List<GoodsSingle> list = shopDAO.findAllGoodsSingles();
                double total = shopDAO.totalMoney();

                request.setAttribute("total", total);
                request.setAttribute("list", list);
                request.getRequestDispatcher("admin/shopcar.jsp").forward(request, response);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
